﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using mesoBoard.Data.Repositories;
using mesoBoard.Data;
using System.Text.RegularExpressions;
using mesoBoard.Web;
using mesoBoard.Services;
using Ninject;
using Ninject.Web.Mvc;

namespace mesoBoard.Web.Core
{
    public class mesoBoardController : Controller
    {
        public Theme CurrentTheme { get; set; }
        public User CurrentUser { get; private set; }

        public ServicesWrapper mbServices {get; set;}

        public RepositoriesWrapper Repositories { get; set; }

        [Inject]
        public mbEntities db { get; set; }

        public mesoBoardController()
        {

        }

        protected override void Execute(System.Web.Routing.RequestContext requestContext)
        {
            this.Repositories = new RepositoriesWrapper(this.db);
            this.mbServices = new ServicesWrapper(this.Repositories);
            bool IsAuthenticated = requestContext.HttpContext.User.Identity.IsAuthenticated;
            string controllerName = requestContext.RouteData.GetRequiredString("controller");

            if (IsAuthenticated)
                this.CurrentUser = mbServices.Users.GetUser(int.Parse(requestContext.HttpContext.User.Identity.Name));
            else
                this.CurrentUser = new Data.User { UserID = 0 };

            string previewTheme = (string)requestContext.HttpContext.Session["ptheme"];

            Theme TheTheme = mbServices.Themes.GetTheme(CurrentUser, controllerName, previewTheme);

            string ThemeFolderPath = "~/Themes/" + TheTheme.FolderName;
            
            CurrentTheme = TheTheme;

            requestContext.HttpContext.Items["mbThemeFolder"] = TheTheme.FolderName;
            requestContext.HttpContext.Items["Theme"] = TheTheme;

            ViewData["mbThemeFolder"] = TheTheme.FolderName;
            ViewData["mbCurrentUser"] = CurrentUser;
            ViewData["mbTheUser"] = CurrentUser;
            ViewData["mbNewMessagesCount"] = Repositories.Messages.GetNewMessages(CurrentUser.UserID).Count();

            ViewData["PreviewTheme"] = new SelectList(
                Repositories.Themes.GetThemes().Where(x => x.VisibleToUsers == true),
                "ThemeID",
                "Name",
                TheTheme.ThemeID);

            string VisitorIP = requestContext.HttpContext.Request.UserHostAddress;

            mbServices.Global.OnlineUserRoutine(CurrentUser, VisitorIP);
           
            base.Execute(requestContext);
        }

        public void SetError(string msg)
        {
            ViewData["Error"] = TempData["Error"] = msg;
        }

        public void SetSuccess(string msg)
        {
            TempData["Success"] = ViewData["Success"] = msg;
        }

        public void SetNotice(string msg)
        {
            TempData["Notice"] = ViewData["Notice"] = msg;
        }

        public void SetCrumbs(string BreadCrumb, string TopBreadCrumb)
        {
            SetTopBreadCrumb(TopBreadCrumb);
            SetBreadCrumb(BreadCrumb);
        }

        public void SetCrumb(string BreadCrumb)
        {
            SetBreadCrumb(BreadCrumb);
        }

        public void SetBreadCrumb(string BreadCrumb)
        {
            ViewData["BreadCrumb"] = BreadCrumb;
        }

        public void SetTopBreadCrumb(string TopBreadCrumb)
        {
            ViewData["TopBreadCrumb"] = TopBreadCrumb;
        }
    }
}
